<?php

namespace my\wechat;

class Chat
{
    private static $_wechat = null;

    public static $error = '';      //错误信息

    private static function wechat()
    {
        $c = [
            'appid' => c('wx_appid'),
            'appsecret' => c('wx_appsecret_im'),
            'im' => true
        ];

        if (self::$_wechat == null)
            self::$_wechat = new QyWeChat($c);

        return self::$_wechat;
    }

    /**
     * 创建会话
     * @param $chatid
     * @param $name
     * @return bool|mixed
     * @author wb <pithyone@vip.qq.com>
     */
    public static function add($chatid, $name)
    {
        $chat_default_userid = c('chat_default_userid');

        $data = [
            "chatid" => $chatid,
            "name" => $name,
            "owner" => $chat_default_userid[0],
            "userlist" => $chat_default_userid
        ];

        $ret = self::wechat()->createChat($data);

        if ($ret === false) {
            self::$error = QyErrCode::getErrText(self::wechat()->errCode);
            return false;
        }

        return $ret;
    }

    /**
     * 获取会话详情
     * @param $chatid
     * @return bool|mixed
     * @author wb <pithyone@vip.qq.com>
     */
    public static function get($chatid)
    {
        $ret = self::wechat()->getChatInfo($chatid);

        if ($ret === false) {
            self::$error = QyErrCode::getErrText(self::wechat()->errCode);
            return false;
        }

        return $ret;
    }

    /**
     * 修改会话信息
     * @param $data
     * @return bool|mixed
     * @author wb <pithyone@vip.qq.com>
     */
    public static function update($data)
    {
        $ret = self::wechat()->updateChat($data);

        if ($ret === false) {
            self::$error = QyErrCode::getErrText(self::wechat()->errCode);
            return false;
        }

        return $ret;
    }

    /**
     * 修改会话标题
     * @param $chatid
     * @param $name
     * @return bool|mixed
     * @author wb <pithyone@vip.qq.com>
     */
    public static function updateName($chatid, $name)
    {
        $data = [
            "chatid" => $chatid,
            "op_user" => c('chat_default_userid')[0],
            "name" => $name
        ];

        $ret = self::wechat()->updateChat($data);

        if ($ret === false) {
            self::$error = QyErrCode::getErrText(self::wechat()->errCode);
            return false;
        }

        return $ret;
    }

    /**
     * 修改会话管理员
     * @param $chatid
     * @param $owner
     * @return bool|mixed
     * @author wb <pithyone@vip.qq.com>
     */
    public static function updateOwner($chatid, $owner)
    {
        $data = [
            "chatid" => $chatid,
            "op_user" => c('chat_default_userid')[0],
            "owner" => $owner
        ];

        $ret = self::wechat()->updateChat($data);

        if ($ret === false) {
            self::$error = QyErrCode::getErrText(self::wechat()->errCode);
            return false;
        }

        return $ret;
    }

    /**
     * 新增会话成员
     * @param $chatid
     * @param $users array 成员数组
     * @return bool|mixed
     * @author wb <pithyone@vip.qq.com>
     */
    public static function addUser($chatid, $users)
    {
        $data = [
            "chatid" => $chatid,
            "op_user" => c('chat_default_userid')[0],
            "add_user_list" => $users
        ];

        $ret = self::wechat()->updateChat($data);

        if ($ret === false) {
            self::$error = QyErrCode::getErrText(self::wechat()->errCode);
            return false;
        }

        return $ret;
    }

    /**
     * 删除会话成员
     * @param $chatid
     * @param $users array 成员数组
     * @return bool|mixed
     * @author wb <pithyone@vip.qq.com>
     */
    public static function delUser($chatid, $users)
    {
        $data = [
            "chatid" => $chatid,
            "op_user" => c('chat_default_userid')[0],
            "del_user_list" => $users
        ];

        $ret = self::wechat()->updateChat($data);

        if ($ret === false) {
            self::$error = QyErrCode::getErrText(self::wechat()->errCode);
            return false;
        }

        return $ret;
    }

    /**
     * 发送单聊消息
     * @param $sender
     * @param $receiver
     * @param $content
     * @return bool|mixed
     * @author wb <pithyone@vip.qq.com>
     */
    public static function sendSingle($sender, $receiver, $content)
    {
        $data = [
            "receiver" => ['type' => 'single', 'id' => $receiver],
            "sender" => $sender,
            "msgtype" => 'text',
            "text" => ['content' => $content]
        ];

        $ret = self::wechat()->sendChat($data);

        if ($ret === false) {
            self::$error = QyErrCode::getErrText(self::wechat()->errCode);
            return false;
        }

        return $ret;
    }

    /**
     * 发送群聊消息
     * @param $sender
     * @param $chatid
     * @param $content
     * @return bool|mixed
     * @author wb <pithyone@vip.qq.com>
     */
    public static function sendGroup($sender, $chatid, $content)
    {
        $data = [
            "receiver" => ['type' => 'group', 'id' => $chatid],
            "sender" => $sender,
            "msgtype" => 'text',
            "text" => ['content' => $content]
        ];

        $ret = self::wechat()->sendChat($data);

        if ($ret === false) {
            self::$error = QyErrCode::getErrText(self::wechat()->errCode);
            return false;
        }

        return $ret;
    }
}